<?php
namespace Api\Controller;

class ArticleController extends CommonController{
	//列表接口
	public function index(){
		//分页参数
		$page      = $this->_get('page', '', 1);
		$pagesize  = $this->_get('pagesize', '', 10);
		$first_row = $pagesize * ($page - 1);

		//栏目频道
		$channel_id = $this->_get('channel_id');
		if($channel_id){
			$where['channel_id'] = $channel_id;
		}
		//最新文章
		$is_new = $this->_get('is_new');
		if($is_new){
			$order['edit_time'] = 'desc';
		}
		//热门文章
		$is_hot = $this->_get('is_hot');
		if($is_hot){
			$order['view_num'] = 'desc';
		}
		//默认排序
		if(!$order){
			$order['order_id']  = 'asc';
			$order['edit_time'] = 'desc';
			$order['id']        = 'desc';
		}

		$where['status']    = 1;
		$where['is_show']   = 1;
		$where['is_delete'] = 0;
		
		//用户自己的
		$is_user_self = $this->_get('is_user_self');
		if($is_user_self){
			$where['user_id'] = $this->user_id();
			unset($where['status'], $where['is_show']);
			//上线
			if($this->_get('is_on')){
				$where['is_show'] = 1;
			}
			//下线
			if($this->_get('is_off')){
				$where['is_show'] = 0;
			}
			//按状态
			$status = $this->_get('status');
			switch ($status) {
				case 'pass':
					$where['status'] = 1;
					break;

				case 'wait':
					$where['status'] = 2;
					break;

				case 'reject':
					$where['status'] = 3;
					break;
			}
		}

		$count = D('Article')->where($where)->count();
		$list  = D('Article')->where($where)->limit($first_row . ',' . $pagesize)->order($order)->select();
		foreach ($list as $rs) {
			$new_list[] = D('Article')->filter_field($rs);
		}
		$data['list'] = $new_list ? $new_list : array();
		$data['page_info'] = $this->page_info($count,$pagesize);


		if($new_list){
			$this->success('获取数据成功！', $data);
		}else{
			$this->success('找不到相关数据!', $data);
		}
	}
	//文章详情内容
	public function detail(){
		$user_id    = $this->user_id(false);
		$article_id = $this->_get('article_id','文章ID不能为空！');

		$where['id']        = $article_id;
		$where['is_show']   = 1;
		$where['status']    = 1;
		$where['is_delete'] = 0;

		//用户自己的
		$is_user_self = $this->_get('is_user_self');
		if($is_user_self){
			$where['user_id'] = $this->user_id();
			unset($where['status'], $where['is_show']);
		}

		$data = D('Article')->where($where)->find();
		if(!$data){
			$this->error('您请求数据不存在！');
		}

		$data = D('Article')->filter_field($data);
		//用户是否收藏
		$data['is_fav'] = D('User')->is_fav($user_id, $article_id, 'article');
		//是否领取阅读金额
		$data['is_get_read_money']  = D('Article')->is_get_money($user_id, $data['article_id'], 'read_article');
		//是否领取分享金额
		$data['is_get_share_money'] = D('Article')->is_get_money($user_id, $data['article_id'], 'share_article');

		$domain  = C('FILE_SERVICE_DOMAIN');
		$content = str_replace(array('/Uploads/'), $domain . '/Uploads/', $data['content']);

		// D('Account')->recharge($user_id, 'weixin', 88.56, '2015102910109111');

		//更新查看次数
		D('Article')->update_view_num($article_id, $user_id);

		$this->success('获取数据成功！', $data);
	}
	//添加文章
	public function add(){
		$data['user_id']     = $this->user_id();
		$data['title']       = $this->_post('title', '标题不能为空！');
		$data['channel_id']  = $this->_post('channel_id', '请选择栏目！');
		$data['description'] = str_cut($this->_post('description'), 200, '');
		$data['cover_id']    = $this->_post('cover_id', '', 0);

		$data['is_reward']          = $this->_post('is_reward', '', 0);//开启打赏(给自己)
		$data['is_share_to_money']  = $this->_post('is_share_to_money', '', 0);//分享给钱(给用户) 
		$data['is_read_to_money']   = $this->_post('is_read_to_money', '', 0);//开启打赏(给自己)
		$data['share_price']        = $this->_post('share_price', '', 0);//每个分享价格
		$data['share_number_limit'] = $this->_post('share_number_limit', '', 0);//分享数量限制
		$data['read_price']         = $this->_post('read_price', '', 0);//每个阅读价格
		$data['read_number_limit']  = $this->_post('read_number_limit', '', 0);//阅读数量限制

		$data['create_time'] = time();
		$data['edit_time']   = time();
		$data['status']      = 2;
		//评论开头
		$is_comment = $this->_post('is_comment', '', 1);
		$data['is_comment']  = D('Common')->get_config_val('is_comment', 'comment') == 1 ? ($is_comment) : 0;

		$content = htmlspecialchars_decode($this->_post('content', '内容不能为空！'));

		if(D('Article')->add($data)){
			unset($data);
			$article_id = D('Article')->getLastInsID();

			$data['article_id'] = $article_id;
			$data['content']    = $content;
			D('ArticleContent')->add($data);

			$info = D('Article')->info($article_id);
			$this->success('添加成功，请等待审核！', $info);
		}
		$this->error('添加失败，请稍候再试！');
	}
	public function edit(){
		$user_id    = $this->user_id();
		$article_id = $this->_post('article_id', '文章ID不能为空！');

		$data['title']       = $this->_post('title', '标题不能为空！');
		$data['channel_id']  = $this->_post('channel_id', '请选择栏目！');
		$data['description'] = str_cut($this->_post('description'), 200, '');
		$data['edit_time']   = time();
		$data['cover_id']    = $this->_post('cover_id', '', 0);
		$data['status']      = 2;

		$data['is_reward']          = $this->_post('is_reward', '', 0);//开启打赏(给自己)
		$data['is_share_to_money']  = $this->_post('is_share_to_money', '', 0);//分享给钱(给用户) 
		$data['is_read_to_money']   = $this->_post('is_read_to_money', '', 0);//开启打赏(给自己)
		$data['share_price']        = $this->_post('share_price', '', 0);//每个分享价格
		$data['share_number_limit'] = $this->_post('share_number_limit', '', 0);//分享数量限制
		$data['read_price']         = $this->_post('read_price', '', 0);//每个阅读价格
		$data['read_number_limit']  = $this->_post('read_number_limit', '', 0);//阅读数量限制

		//评论开头
		$is_comment = $this->_post('is_comment');
		$is_comment = $is_comment ? 1 : 0;
		$data['is_comment']  = D('Common')->get_config_val('is_comment', 'comment') == 1 ? ($is_comment) : 0;
		
		$content = htmlspecialchars_decode($this->_post('content', '内容不能为空！'));

		$where['id'] = $article_id;
		$info = D('Article')->where($where)->find();
		if($info['user_id'] != $user_id){
			$this->error('对不起，您的操作有误！');
		}

		//保存
		if(D('Article')->where($where)->save($data)){
			unset($data, $where);
			$where['article_id'] = $article_id;
			if(D('ArticleContent')->where($where)->count()){
				$data['content'] = $content;
				D('ArticleContent')->where($where)->save($data);
			}else{
				$data['content']    = $content;
				$data['article_id'] = $article_id;
				D('ArticleContent')->add($data);
			}

			$info = D('Article')->info($article_id);
			$this->success('修改成功', $info);
		}
		$this->error('修改失败，请稍候再试！');
	}
	//设置上线
	public function set_on(){
		$user_id    = $this->user_id();
		$article_id = $this->_post('article_id', '文章ID不能为空！');

		$where['id'] = $article_id;
		$info = D('Article')->where($where)->find();
		if($info['user_id'] != $user_id){
			$this->error('对不起，您的操作有误！');
		}

		$data['is_show'] = 1;
		D('Article')->where($where)->save($data);

		$this->success('上线成功！');
	}
	//设置下线
	public function set_off(){
		$user_id    = $this->user_id();
		$article_id = $this->_post('article_id', '文章ID不能为空！');

		$where['id'] = $article_id;
		$info = D('Article')->where($where)->find();
		if($info['user_id'] != $user_id){
			$this->error('对不起，您的操作有误！');
		}

		$data['is_show'] = 0;
		D('Article')->where($where)->save($data);

		$this->success('下线成功！');
	}
	//删除文章
	public function delete(){
		$user_id    = $this->user_id();
		$article_id = $this->_post('article_id', '文章ID不能为空！');

		$where['id'] = $article_id;
		$info = D('Article')->where($where)->find();
		if($info['user_id'] != $user_id){
			$this->error('对不起，您的操作有误！');
		}

		//删除操作
		$data['is_delete']    = 1;
		$data['recycle_time'] = time();
		D('Article')->where($where)->save($data);

		$this->success('删除成功！');
	}
	//添加/取消收藏
	public function set_fav(){
		$user_id    = $this->user_id();
		$article_id = $this->_post('article_id', '文章ID不能为空！');

		$where['id'] = $article_id;
		$info = D('Article')->where($where)->find();
		if(!$info || $info['is_delete'] == 1){
			$this->error('对不起，文章不存在！');
		}

		$return_id = D('User')->set_fav($user_id, $article_id, 'article');

		if($return_id == 1){
			$this->success('添加收藏成功！');
		}else{
			$this->success('取消收藏成功！');
		}
	}
	//收藏列表
	public function fav_list(){
		$user_id   = $this->user_id();
		//分页参数
		$page      = $this->_get('page', '', 1);
		$pagesize  = $this->_get('pagesize', '', 10);
		$first_row = $pagesize * ($page - 1);


		$info  = D('User')->get_fav($user_id, 'article', $page, $pagesize);
		$count = $info['count'];
		$list  = $info['list'];
		foreach ($list as $rs) {
			$row = D('Article')->info($rs['fav_id']);
			$row['fav_desc'] = date('Y-m-d H:i:s', $rs['create_time']);
			$new_list[] = $row;
		}
		$data['list']      = $new_list ? $new_list : array();
		$data['page_info'] = $this->page_info($count, $pagesize);
		
		$this->success($new_list ? '获取数据成功！' : '您目前没有相关收藏！', $data);
	}
	//提交评价
	public function comment_submit(){
		$user_id    = $this->user_id();
		$article_id = $this->_post('article_id', '文章ID不能为空！');
		$content    = $this->_post('content', '内容不能为空！');
		$reply_id   = $this->_post('reply_id');

		if(D('Comment')->submit('article', $user_id, $article_id, $content, $reply_id)){
			//更新评价数量
			if(!D('Common')->get_config_val('is_audit', 'comment')){
				D('Article')->update_num($article_id, 'comment_num');
			}
			$this->success('提交成功！');
		}
		$this->error('提交失败！');
	}
	//评价列表
	public function comment_list(){
		$user_id    = $this->user_id(false);
		$article_id = $this->_get('article_id', '文章ID不能为空！');
		//分页参数
		$page      = $this->_get('page', '', 1);
		$pagesize  = $this->_get('pagesize', '', 10);
		$first_row = $pagesize * ($page - 1);

		$where['type']     = 'article';
		$where['other_id'] = $article_id;
		$where['_string'] = "status = 1 or user_id = '$user_id'";

		$order['id'] = 'desc';

		$count = D('Comment')->where($where)->count();
		$list  = D('Comment')->where($where)->limit($first_row . ',' . $pagesize)->order($order)->select();
		foreach ($list as $rs) {
			$new_list[] = D('Comment')->filter_field($rs);
		}
		$data['list'] = $new_list ? $new_list : array();
		$data['page_info'] = $this->page_info($count, $pagesize);

		if($new_list){
			$this->success('获取数据成功！', $data);
		}else{
			$this->success('找不到相关数据!', $data);
		}
	}
	//领取阅读金额
	public function get_read_money(){
		$user_id    = $this->user_id();
		$article_id = $this->_post('article_id', '文章ID不能为空！');

		$where['id'] = $article_id;
		$info = D('Article')->where($where)->find();
		if(!$info || $info['is_delete'] == 1 || $info['is_show'] != 1){
			$this->error('对不起，文章不存在！');
		}
		if(!$info['is_read_to_money']){
			$this->error('对不起，此文章没有开启阅读领赏功能！');
		}
		if(!$info['user_id']){
			$this->error('对不起，此文章没有开启阅读领赏功能！');
		}
		if(!$info['read_price']){
			$this->error('对不起，此文章未设置领赏金额！');
		}
		if(!$info['read_number_limit']){
			$this->error('对不起，此文章未设置领赏数量！');
		}
		if($info['read_number_limit'] <= $info['read_price_number']){
			$this->error('不好意思，您来晚了！');
		}
		$article_user_money = D('Account')->get_account_money($user_id);
		if($info['read_price'] > $article_user_money){
			$this->error('对不起，您来晚了！');
		}
		if(D('Article')->is_get_money($user_id, $article_id, 'read_article')){
			$this->error('对不起，您已经领取过了！');
		}
		//获取阅读文章金额给用户
		if(D('Account')->read_article($user_id, $info['user_id'], $article_id, $info['read_price'], $info['title'])){
			//更新已阅读金额数量
			D('Article')->update_num($article_id, 'read_price_number');
			//记录阅读文章获取金额
			D('Article')->record_get_money($user_id, $article_id, $info['read_price'], 'read_article');
		}

		$this->success('领取阅读文章赏钱' . $info['read_price'] . '元');
	}
}
?>